home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / plnk081.zip / pilot-link.0.8.1 / reminders.c < prev    next >
C/C++ Source or Header  |  1997-08-06  |  7KB  |  274 lines

  1. /* reminders.c:  Translate Pilot datebook into REMIND format
  2.  *
  3.  * Copyright (c) 1996, Kenneth Albanowski
  4.  *
  5.  * This is free software, licensed under the GNU Public License V2.
  6.  * See the file COPYING for details.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "pi-source.h"
  13. #include "pi-socket.h"
  14. #include "pi-datebook.h"
  15. #include "pi-dlp.h"
  16.  
  17. char *Weekday[7] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
  18. char *Month[12] = {"jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"};
  19.                   
  20. int main(int argc, char *argv[])
  21. {
  22.   struct pi_sockaddr addr;
  23.   int db;
  24.   int sd;
  25.   int i;
  26.   struct PilotUser U;
  27.   int ret;
  28.   unsigned char buffer[0xffff];
  29.  
  30.   if (argc < 2) {
  31.     fprintf(stderr,"usage:%s %s\n",argv[0],TTYPrompt);
  32.     exit(2);
  33.   }
  34.   if (!(sd = pi_socket(PI_AF_SLP, PI_SOCK_STREAM, PI_PF_PADP))) {
  35.     perror("pi_socket");
  36.     exit(1);
  37.   }
  38.     
  39.   addr.pi_family = PI_AF_SLP;
  40.   strcpy(addr.pi_device,argv[1]);
  41.   
  42.   ret = pi_bind(sd, (struct sockaddr*)&addr, sizeof(addr));
  43.   if(ret == -1) {
  44.     perror("pi_bind");
  45.     exit(1);
  46.   }
  47.  
  48.   ret = pi_listen(sd,1);
  49.   if(ret == -1) {
  50.     perror("pi_listen");
  51.     exit(1);
  52.   }
  53.  
  54.   sd = pi_accept(sd, 0, 0);
  55.   if(sd == -1) {
  56.     perror("pi_accept");
  57.     exit(1);
  58.   }
  59.  
  60.   /* Ask the pilot who it is. */
  61.   dlp_ReadUserInfo(sd,&U);
  62.   
  63.   /* Tell user (via Pilot) that we are starting things up */
  64.   dlp_OpenConduit(sd);
  65.   
  66.   /* Open the Datebook's database, store access handle in db */
  67.   if(dlp_OpenDB(sd, 0, 0x80|0x40, "DatebookDB", &db) < 0) {
  68.     puts("Unable to open DatebookDB");
  69.     dlp_AddSyncLogEntry(sd, "Unable to open DatebookDB.\n");
  70.     pi_close(sd);
  71.     exit(1);
  72.   }
  73.  
  74.   printf("PUSH-OMIT-CONTEXT\n");  
  75.   printf("CLEAR-OMIT-CONTEXT\n");  
  76.   for (i=0;1;i++) {
  77.       int j;
  78.       struct Appointment a;
  79.       int attr;
  80.       char delta[80];
  81.       char satisfy[256];
  82.                                  
  83.       int len = dlp_ReadRecordByIndex(sd, db, i, buffer, 0, 0, &attr, 0);
  84.       if(len<0)
  85.           break;
  86.           
  87.       /* Skip deleted records */
  88.       if((attr & dlpRecAttrDeleted) || (attr & dlpRecAttrArchived))
  89.           continue;
  90.           
  91.     unpack_Appointment(&a, buffer, len);
  92.  
  93.     strcpy(delta, "+7 ");
  94.     satisfy[0] = 0;
  95.     
  96.     if (a.exceptions) {
  97.         printf("PUSH-OMIT-CONTEXT\n");
  98.         for(j=0;j<a.exceptions;j++) {
  99.             printf("OMIT %d %s %d\n", a.exception[j].tm_mday,
  100.                         Month[a.exception[j].tm_mon],
  101.                         a.exception[j].tm_year+1900);
  102.         }
  103.     }
  104.     
  105.     
  106.     if (a.advance) {
  107.         sprintf(delta + strlen(delta), "AT %2.2d:%2.2d +%d ",
  108.             a.begin.tm_hour,a.begin.tm_min, 
  109.             a.advance * ( 
  110.                 (a.advanceUnits == advMinutes) ? 1 :
  111.                 (a.advanceUnits == advHours) ? 60 :
  112.                 (a.advanceUnits == advDays) ? 60*24 :
  113.                 0));
  114.     }
  115.     
  116.     if (!a.repeatForever) {
  117.         sprintf(delta + strlen(delta), "UNTIL %d %s %d ", 
  118.             a.repeatEnd.tm_mday, Month[a.repeatEnd.tm_mon], a.repeatEnd.tm_year+1900);
  119.     }
  120.  
  121.     if(a.repeatFrequency) {
  122.         if(a.repeatType == repeatDaily) {
  123.                 /* On the specified day... */
  124.             printf("REM %d %s %d ",a.begin.tm_mday,Month[a.begin.tm_mon],a.begin.tm_year+1900);
  125.             if(a.repeatFrequency > 1) {
  126.                 /* And every x days afterwords */
  127.                 printf("*%d ",a.repeatFrequency);
  128.             }
  129.         } else if(a.repeatType == repeatMonthlyByDate) {
  130.             /* On the x of every month */
  131.             printf("REM %d ", a.begin.tm_mday);
  132.  
  133.             if(a.repeatFrequency>1) {
  134.  
  135.                 /* if the month is equal to the starting month mod x */
  136.                 sprintf(satisfy,"SATISFY \
  137. [(trigdate()>=date(%d,%d,%d)) && \
  138.  (!isomitted(trigdate())) && \
  139. (((monnum(trigdate())-1+year(trigdate())*12)%%%d) == ((%d+%d*12)%%%d))] ",
  140.                     a.begin.tm_year+1900,
  141.                     a.begin.tm_mon+1,
  142.                     a.begin.tm_mday,
  143.                     a.repeatFrequency,
  144.                     a.begin.tm_year+1900,
  145.                     a.begin.tm_mon,
  146.                     a.repeatFrequency);
  147.             } else {
  148.                 sprintf(satisfy, "SATISFY [(trigdate()>=date(%d,%d,%d)) && (!isomitted(trigdate()))] ",
  149.                     a.begin.tm_year+1900,
  150.                     a.begin.tm_mon+1,
  151.                     a.begin.tm_mday);
  152.             }
  153.         } else if(a.repeatType == repeatWeekly) {
  154.             int k;
  155.             /* On the chosen days of the week */
  156.             printf("REM ");
  157.             for(k=0;k<7;k++)
  158.                 if(a.repeatDays[i])
  159.                     printf("%s ", Weekday[i]);
  160.  
  161.             if(a.repeatFrequency>1) {
  162.                 /* if the week is equal to the starting week mod x */
  163.                 sprintf(satisfy, "SATISFY \
  164. [(trigdate()>=date(%d,%d,%d)) &&\
  165.   (!isomitted(trigdate())) &&\
  166.   (((coerce(\"int\",trigdate())/7)%%%d) == ((coerce(\"int\",date(%d,%d,%d))/7)%%%d))] ",
  167.         a.begin.tm_year+1900,
  168.         a.begin.tm_mon+1,
  169.         a.begin.tm_mday,
  170.         a.repeatFrequency,
  171.         a.begin.tm_year+1900,
  172.         a.begin.tm_mon+1,
  173.         a.begin.tm_mday,
  174.         a.repeatFrequency);
  175.             } else {
  176.                 sprintf(satisfy, "SATISFY [(trigdate()>=date(%d,%d,%d))  && (!isomitted(trigdate()))] ",
  177.                     a.begin.tm_year+1900,
  178.                     a.begin.tm_mon+1,
  179.                     a.begin.tm_mday);
  180.             }
  181.         } else if(a.repeatType == repeatMonthlyByDay) {
  182.             int day;
  183.             int weekday;
  184.             
  185.             if(a.repeatDay>=domLastSun) {
  186.                 day = 1;
  187.                 weekday = a.repeatDay % 7;
  188.                 printf("REM %s %d -7 ", Weekday[weekday], day);
  189.             } else {
  190.                 day = a.repeatDay / 7 * 7 + 1;
  191.                 weekday = a.repeatDay % 7;
  192.                 printf("REM %s %d ", Weekday[weekday], day);
  193.             }
  194.  
  195.             if( a.repeatFrequency > 1) {
  196.  
  197.                 sprintf(satisfy,"SATISFY \
  198. [(trigdate()>=date(%d,%d,%d)) && \
  199.  (!isomitted(trigdate())) && \
  200. (((monnum(trigdate())-1+year(trigdate())*12)%%%d) == ((%d+%d*12)%%%d))] ",
  201.                     a.begin.tm_year+1900,
  202.                     a.begin.tm_mon+1,
  203.                     a.begin.tm_mday,
  204.                     a.repeatFrequency,
  205.                     a.begin.tm_year+1900,
  206.                     a.begin.tm_mon,
  207.                     a.repeatFrequency);
  208.             } else {
  209.                 sprintf(satisfy, "SATISFY [(trigdate()>=date(%d,%d,%d))  && (!isomitted(trigdate()))] ",
  210.                     a.begin.tm_year+1900,
  211.                     a.begin.tm_mon+1,
  212.                     a.begin.tm_mday);
  213.             }
  214.         } else if(a.repeatType == repeatYearly) {
  215.             /* On one day each year */
  216.             printf("REM %d %s ", a.begin.tm_mday, Month[a.begin.tm_mon]);
  217.     
  218.             if(a.repeatFrequency>1) {
  219.  
  220.                 /* if the year is equal to the starting year, mod x */
  221.                 sprintf(satisfy,"SATISFY \
  222. [(trigdate()>=date(%d,%d,%d)) &&\
  223.  (!isomitted(trigdate())) &&\
  224.  ((year(trigdate())%%%d) == (%d%%%d))] ",
  225.                     a.begin.tm_year+1900,
  226.                     a.begin.tm_mon+1,
  227.                     a.begin.tm_mday,
  228.                     a.repeatFrequency,
  229.                     a.begin.tm_year+1900,
  230.                     a.repeatFrequency);
  231.             } else {
  232.                 sprintf(satisfy, "SATISFY [(trigdate()>=date(%d,%d,%d)) && (!isomitted(trigdate()))]",
  233.                     a.begin.tm_year+1900,
  234.                     a.begin.tm_mon+1,
  235.                     a.begin.tm_mday);
  236.             }
  237.         } 
  238.  
  239.     } else {
  240.         /* On that one day */
  241.         printf("REM %d %s %d ",a.begin.tm_mday, Month[a.begin.tm_mon], a.begin.tm_year+1900);
  242.     }                                   
  243.     
  244.     printf("%s%s",delta,satisfy);
  245.     
  246.     printf("MSG %s %%a", a.description);
  247.     if(a.note)
  248.         printf(" (%s)", a.note);
  249.  
  250.     if(!a.event) {
  251.         printf(" from %2.2d:%2.2d", a.begin.tm_hour, a.begin.tm_min);
  252.         printf(" to %2.2d:%2.2d", a.end.tm_hour, a.end.tm_min);
  253.     }
  254.     printf("\n");
  255.  
  256.     if (a.exceptions)
  257.         printf("POP-OMIT-CONTEXT\n");
  258.  
  259.     free_Appointment(&a);
  260.         
  261.   }
  262.   printf("POP-OMIT-CONTEXT\n");
  263.  
  264.   /* Close the database */
  265.   dlp_CloseDB(sd, db);
  266.  
  267.   dlp_AddSyncLogEntry(sd, "Read datebook from Pilot.\n");
  268.  
  269.   pi_close(sd);  
  270.   
  271.   exit(0);
  272. }
  273.  
  274.